﻿#include "langpairstring.h"
#include "../../_global.h"
#include "../containerscommon.h"

#ifdef _DEBUG
//#ifdef _CRTDBG_MAP_ALLOC
#define new new(_NORMAL_BLOCK, __FILE__, __LINE__)
//#endif 
#endif 

LangPairString::LangPairString():DataContainerBase(),
                                 language(SPTR_ALLOC_ON_CALL),
																 caption(SPTR_ALLOC_ON_CALL){

}

LangPairString::LangPairString(__int64 _ID):DataContainerBase(_ID),
                                            language(SPTR_ALLOC_ON_CALL),
																						caption(SPTR_ALLOC_ON_CALL){

}

LangPairString::~LangPairString(){
	
}

long LangPairString::parse(SPtr<FileContainer> _inFileContainer,long _offsetInData){

	long dataSize;
	char *dataBuf=NULL;

  if(preParse(_inFileContainer,_offsetInData,&dataBuf,&dataSize)!=1)return -1;
	if(dataBuf==NULL)return -2;

	//Разбор контейнера для конкретного типа (этого объекта):
	//if(_inFileContainer.p()->getContainerType()!=CT_LANG_PAIR_USTRING)return -3;
	if(dataSize<16)return -4;
	setID(getFileContainer().p()->getID());

	memcpy((void *)(&ID_USTRING_Language),(void *)dataBuf,8);
	memcpy((void *)(&ID_USTRING_Caption),(void *)(dataBuf+8),8);

	return 16;

}

long LangPairString::parse(){
	if(getFileContainer().isNULLPtr())return -1;
	if(getFileContainer().p()->getContainerType()!=CT_LANG_PAIR_USTRING)return -3;
	return parse(getFileContainer(),0);
}

int LangPairString::join(){

	if(preJoin()!=1)return -1;
	if(checkAndSetObjectsAtIDs()<0)return -2;

	//сборка данных в контейнер:
	getFileContainer().p()->setContainerType(CT_LANG_PAIR_USTRING);
	getFileContainer().p()->setID(getID());

	char* data;
	long size;
	
	size=16;
	data=new char[size];

	((__int64 *)data)[0]=ID_USTRING_Language;
	((__int64 *)data)[1]=ID_USTRING_Caption;

	getFileContainer().p()->setDataCopy(data,size);
	delete [] data;
	return 0;

}

int LangPairString::checkAndSetObjectsAtIDs(){
	if(checkAndSet_T_AtID<UString>(&ResManager::getUStringAtID, &language, ID_USTRING_Language)<0)return -1;
	if(checkAndSet_T_AtID<UString>(&ResManager::getUStringAtID, &caption, ID_USTRING_Caption)<0)return -1;
  return 0;
}

SPtr<UString> LangPairString::getLanguage(){
  return language;
}

SPtr<UString> LangPairString::getCaption(){
  return caption;
}

void LangPairString::setLanguage(SPtr<UString> _language){
	language=_language;
	ID_USTRING_Language=language.p()->getID();
}

void LangPairString::setCaption(SPtr<UString> _caption){
	caption=_caption;
	ID_USTRING_Caption=_caption.p()->getID();
}
